package com.bk.bookviews.controller;

import com.bk.bookviews.common.Constants;
import com.bk.bookviews.common.Pagination;
import com.bk.bookviews.domain.ResultMsg;
import com.bk.bookviews.domain.ScenicInfo;
import com.bk.bookviews.domain.UserInfo;
import com.bk.bookviews.exception.BusinessException;
import com.bk.bookviews.exception.BusinessExceptionEnum;
import com.bk.bookviews.service.ScenicInfoService;
import com.bk.bookviews.util.PageUtils;
import com.bk.bookviews.util.RedisUtil;
import com.bk.bookviews.util.StringUtil;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
@RequestMapping("/bk/scenic")
public class ScenicInfoController {
    @Autowired
    RedisUtil redisUtil;

    @Autowired
    ScenicInfoService scenicInfoService;

    @GetMapping()
    public ResultMsg selectScenicInfo(Long scenicId, @RequestHeader("token") String token) throws IOException {
        ScenicInfo scenicInfo = scenicInfoService.selectScenic(scenicId,token);
        return ResultMsg.successResult(scenicInfo);
    }
    @PostMapping("/add")
    public ResultMsg register(@RequestBody ScenicInfo scenicInfo ,@RequestHeader("token") String token) throws BusinessException {
        scenicInfoService.add(scenicInfo,token);
        return ResultMsg.successResult();
    }

    /**
     * 修改
     */
    @PutMapping()
    public ResultMsg editSave(@RequestBody ScenicInfo scenicInfo) throws BusinessException {
        if (scenicInfo.getScenicId() == null){
            throw new BusinessException(BusinessExceptionEnum.SCENIC_ID_IS_NULL);
        }
        scenicInfoService.updateScenic(scenicInfo);
        return ResultMsg.successResult();
    }
    @PostMapping("/del")
    public ResultMsg del(@RequestBody ScenicInfo scenicInfo ,@RequestHeader("token") String token) throws BusinessException {
        scenicInfoService.del(scenicInfo,token);
        return ResultMsg.successResult();
    }
    @PostMapping("/image")
    public ResultMsg uploadFile(MultipartFile file) throws IOException {
        if (file == null){
            throw new BusinessException(BusinessExceptionEnum.UPLOAD_IMAGE_IS_NULL);
        }
        //校验是否为非法图片
        BufferedImage read = ImageIO.read(file.getInputStream());
        if (read == null){
            throw new BusinessException(BusinessExceptionEnum.UPLOAD_FILE_NOT_IMAGE);
        }
        return ResultMsg.successResult(scenicInfoService.uploadFile(file));
    }

    @PostMapping("/scenicList")
    @ResponseBody
    public Pagination selectRoomPage(@RequestBody ScenicInfo scenicInfo, @RequestHeader("token") String token, @RequestHeader("pageNum") String pageNum, @RequestHeader("pageSize") String pageSize) {
        //方法执行前分页
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        if (StringUtils.isNotBlank(token)){
            UserInfo userInfo = (UserInfo) redisUtil.get(token);
            if (userInfo == null){
                throw new BusinessException(BusinessExceptionEnum.LOGON_FAILURE);
            }
            if (Constants.SIMPLE_USER.equals(userInfo.getUserRole())){
                scenicInfo.setSource(Constants.QUERY_SOURCE);
            }else {
                scenicInfo.setSource(Constants.QUERY_SOURCE_BACKGROUND);
            }
        }
        if (StringUtils.isBlank(scenicInfo.getSource())) {
            throw new BusinessException(BusinessExceptionEnum.SOURCE_ISNULL);
        }
        return PageUtils.getPagination(scenicInfoService.selectScenicList(scenicInfo));
    }
}
